Skip to content

Commit

Permalink
feat(core): currentChange cbs get trigger arg (#25) (#42)
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
kirangadhave authored Mar 6, 2023
2 parents b14ad6c + 941ed66 commit 8d7e71f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/check-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Check Release

on: pull_request

env:
HUSKY: 0
GIT_AUTHOR_NAME: kirangadhave
GIT_AUTHOR_EMAIL: [email protected]
GIT_COMMITTER_NAME: kirangadhave
GIT_COMMITTER_EMAIL: [email protected]

jobs:
release:
name: Check Release
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Track main
run: git branch --track main origin/main || echo "Already done"

- name: 'Derive appropriate SHAs for base and head for `nx affected` commands'
uses: nrwl/nx-set-shas@v3

- run: |
echo "BASE: ${{ env.NX_BASE }}"
echo "HEAD: ${{ env.NX_HEAD }}"
- name: Setup LTS Node
uses: actions/setup-node@v3
with:
node-version: 'lts/*'
cache: 'yarn'

- name: Install yarn
run: npm install -g yarn

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT

- name: Cache yarn dependencies
uses: actions/cache@v3
id: yarn-cache
with:
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
**\node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install

- name: Build
run: |
RUN=CI yarn run build-affected-libs
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.TRRACK_GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
RUN=CI yarn run release --dry-run
42 changes: 36 additions & 6 deletions packages/core/src/graph/provenance-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import {
createListenerMiddleware,
isAnyOf,
} from '@reduxjs/toolkit';
import { ID } from '../utils';

import { RootNode } from './components';
import { graphSliceCreator } from './graph-slice';

export type Trigger = 'traversal' | 'new';

export type CurrentChangeHandler = (trigger?: Trigger) => void;
export type CurrentChangeHandlerConfig = {
skipOnNew: boolean;
};
export type UnsubscribeCurrentChangeListener = () => boolean;

export function initializeProvenanceGraph<S>(initialState: S) {
let listeners: any[] = [];
const listeners: Map<
string,
{
id: string;
func: CurrentChangeHandler;
config: CurrentChangeHandlerConfig;
}
> = new Map();

const { reducer, actions, getInitialState } =
graphSliceCreator(initialState);
Expand All @@ -20,7 +36,14 @@ export function initializeProvenanceGraph<S>(initialState: S) {
matcher: isAnyOf(actions.changeCurrent, actions.addNode),
effect: (action, listenerApi) => {
listenerApi.cancelActiveListeners();
listeners.forEach((l) => l());
listeners.forEach((listener) => {
const isNew = isAnyOf(actions.addNode)(action);
const { skipOnNew } = listener.config;

if (skipOnNew && isNew) return;

listener.func(isNew ? 'new' : 'traversal');
});
},
});

Expand All @@ -41,11 +64,18 @@ export function initializeProvenanceGraph<S>(initialState: S) {
get root() {
return store.getState().nodes[store.getState().root] as RootNode<S>;
},
currentChange(listener: any) {
listeners.push(listener);
return () => {
listeners = listeners.filter((l) => l !== listener);
currentChange(
func: CurrentChangeHandler,
config: CurrentChangeHandlerConfig
): UnsubscribeCurrentChangeListener {
const listener = {
id: ID.get(),
func,
config,
};
listeners.set(listener.id, listener);

return () => listeners.delete(listener.id);
},
update: store.dispatch,
...actions,
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/provenance/trrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { initEventManager } from '../event';
import {
BaseArtifactType,
createStateNode,
CurrentChangeHandler,
initializeProvenanceGraph,
isStateNode,
NodeId,
Expand All @@ -14,6 +15,7 @@ import {
SideEffects,
StateLike,
StateNode,
UnsubscribeCurrentChangeListener,
} from '../graph';
import { ProvenanceGraph } from '../graph/graph-slice';
import {
Expand Down Expand Up @@ -256,7 +258,7 @@ export function initializeTrrack<State = any, Event extends string = string>({
return Promise.resolve(console.warn('Already at root!'));
}
},
redo(to?: 'latest' | 'oldest') {
redo(to: 'latest' | 'oldest' = 'latest') {
const { current } = graph;
if (current.children.length > 0) {
return this.to(
Expand All @@ -270,8 +272,13 @@ export function initializeTrrack<State = any, Event extends string = string>({
);
}
},
currentChange(listener: any) {
return graph.currentChange(listener);
currentChange(
listener: CurrentChangeHandler,
skipOnNew = false
): UnsubscribeCurrentChangeListener {
return graph.currentChange(listener, {
skipOnNew,
});
},
done() {
console.log('Setup later for URL sharing.');
Expand Down
3 changes: 2 additions & 1 deletion packages/redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"@reduxjs/toolkit": "^1.9.1"
},
"peerDependencies": {
"@reduxjs/toolkit": "^1.9.1"
"@reduxjs/toolkit": "^1.9.1",
"@trrack/core": "^1.0.0"
},
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion packages/redux/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('it', () => {
it('should work', () => {
const getPostById = createAsyncThunk(
'test/getpostById',
async (postId: number, api) => {
async (postId: number, _api) => {
const response = await fetch(
` https://jsonplaceholder.typicode.com/posts/${postId} `
);
Expand Down

0 comments on commit 8d7e71f

Please sign in to comment.