From 1f0752f544e1e49a45f1339921a52a2df34de530 Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Sun, 5 Mar 2023 17:47:02 -0700 Subject: [PATCH 1/5] feat(core): currentChange cbs get trigger arg (#25) currentChange function takes in a second argument which configures if callback is to be skipped based when adding new node --- packages/core/src/graph/provenance-graph.ts | 42 ++++++++++++++++++--- packages/core/src/provenance/trrack.ts | 13 +++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/core/src/graph/provenance-graph.ts b/packages/core/src/graph/provenance-graph.ts index ee1aa06..e3b19b1 100644 --- a/packages/core/src/graph/provenance-graph.ts +++ b/packages/core/src/graph/provenance-graph.ts @@ -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(initialState: S) { - let listeners: any[] = []; + const listeners: Map< + string, + { + id: string; + func: CurrentChangeHandler; + config: CurrentChangeHandlerConfig; + } + > = new Map(); const { reducer, actions, getInitialState } = graphSliceCreator(initialState); @@ -20,7 +36,14 @@ export function initializeProvenanceGraph(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'); + }); }, }); @@ -41,11 +64,18 @@ export function initializeProvenanceGraph(initialState: S) { get root() { return store.getState().nodes[store.getState().root] as RootNode; }, - 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, diff --git a/packages/core/src/provenance/trrack.ts b/packages/core/src/provenance/trrack.ts index d621252..fb7249c 100644 --- a/packages/core/src/provenance/trrack.ts +++ b/packages/core/src/provenance/trrack.ts @@ -6,6 +6,7 @@ import { initEventManager } from '../event'; import { BaseArtifactType, createStateNode, + CurrentChangeHandler, initializeProvenanceGraph, isStateNode, NodeId, @@ -14,6 +15,7 @@ import { SideEffects, StateLike, StateNode, + UnsubscribeCurrentChangeListener, } from '../graph'; import { ProvenanceGraph } from '../graph/graph-slice'; import { @@ -256,7 +258,7 @@ export function initializeTrrack({ 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( @@ -270,8 +272,13 @@ export function initializeTrrack({ ); } }, - 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.'); From add8e970d5b64b951977e6b505224fbf00c3db6c Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Sun, 5 Mar 2023 18:10:30 -0700 Subject: [PATCH 2/5] fix(redux): fixed export in redux package.json --- packages/redux/package.json | 3 ++- packages/redux/tests/index.spec.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/redux/package.json b/packages/redux/package.json index 7b4b922..a89eb65 100644 --- a/packages/redux/package.json +++ b/packages/redux/package.json @@ -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": { ".": { diff --git a/packages/redux/tests/index.spec.ts b/packages/redux/tests/index.spec.ts index ebda501..dcb8a0c 100644 --- a/packages/redux/tests/index.spec.ts +++ b/packages/redux/tests/index.spec.ts @@ -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} ` ); From 2a26a34ee6bb1657989dc606d2c99136e281a718 Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Sun, 5 Mar 2023 18:16:41 -0700 Subject: [PATCH 3/5] ci: added a check-release workflow --- .github/workflows/check-release.yml | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 .github/workflows/check-release.yml diff --git a/.github/workflows/check-release.yml b/.github/workflows/check-release.yml new file mode 100644 index 0000000..65cd447 --- /dev/null +++ b/.github/workflows/check-release.yml @@ -0,0 +1,71 @@ +name: Test -> Build -> Release + +on: pull_request + +env: + HUSKY: 0 + GIT_AUTHOR_NAME: kirangadhave + GIT_AUTHOR_EMAIL: kirangadhave2@gmail.com + GIT_COMMITTER_NAME: kirangadhave + GIT_COMMITTER_EMAIL: kirangadhave2@gmail.com + +jobs: + release: + name: Test -> Build -> 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 + if: ${{ success() && (github.event_name != 'pull_request' || github.event.action == 'closed' && github.event.pull_request.merged == true) }} + env: + GITHUB_TOKEN: ${{ secrets.TRRACK_GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + RUN=CI yarn run release --dry-run From 9d9e669bbb98ea8ed83ccee9493e7bfd876e856f Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Sun, 5 Mar 2023 18:18:00 -0700 Subject: [PATCH 4/5] ci: fixed workflow name --- .github/workflows/check-release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-release.yml b/.github/workflows/check-release.yml index 65cd447..d6a3769 100644 --- a/.github/workflows/check-release.yml +++ b/.github/workflows/check-release.yml @@ -1,4 +1,4 @@ -name: Test -> Build -> Release +name: Check Release on: pull_request @@ -11,7 +11,7 @@ env: jobs: release: - name: Test -> Build -> Release + name: Check Release runs-on: ubuntu-latest steps: From 941ed66cd00d21bb07d483f666164b4b87585624 Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Sun, 5 Mar 2023 18:20:25 -0700 Subject: [PATCH 5/5] ci: removed trigger check from check-release --- .github/workflows/check-release.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/check-release.yml b/.github/workflows/check-release.yml index d6a3769..cf71df2 100644 --- a/.github/workflows/check-release.yml +++ b/.github/workflows/check-release.yml @@ -63,7 +63,6 @@ jobs: RUN=CI yarn run build-affected-libs - name: Release - if: ${{ success() && (github.event_name != 'pull_request' || github.event.action == 'closed' && github.event.pull_request.merged == true) }} env: GITHUB_TOKEN: ${{ secrets.TRRACK_GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }}