-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extensions): added trace extension
- Loading branch information
1 parent
19a6335
commit 2835c45
Showing
9 changed files
with
278 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<p align="center"> | ||
<a href="https://harlemjs.com"> | ||
<img src="https://raw.githubusercontent.com/andrewcourtice/harlem/main/docs/src/.vuepress/public/assets/images/logo-192.svg" alt="Harlem"/> | ||
</a> | ||
</p> | ||
|
||
# Harlem Snapshot Plugin | ||
|
||
![npm](https://img.shields.io/npm/v/@harlem/plugin-snapshot) | ||
|
||
This is the official Harlem plugin for taking state snapshots and applying them when convenient. | ||
|
||
<!-- TOC depthfrom:2 --> | ||
|
||
- [Getting started](#getting-started) | ||
|
||
<!-- /TOC --> | ||
|
||
## Getting started | ||
|
||
Before installing the snapshot plugin make sure you have installed `@harlem/core`. | ||
|
||
1. Install `@harlem/plugin-snapshot`: | ||
``` | ||
npm install @harlem/plugin-snapshot | ||
``` | ||
Or if you're using Yarn: | ||
``` | ||
yarn add @harlem/plugin-snapshot | ||
``` | ||
|
||
2. Create an instance of the plugin and register it with Harlem: | ||
```typescript | ||
import App from './app.vue'; | ||
|
||
import harlem from '@harlem/core'; | ||
import createSnapshotPlugin from '@harlem/plugin-snapshot'; | ||
|
||
createApp(App) | ||
.use(harlem, { | ||
plugins: [ | ||
createSnapshotPlugin() | ||
] | ||
}) | ||
.mount('#app'); | ||
``` | ||
|
||
3. Call the snapshot method with the name of the store you wish to snapshot: | ||
```typescript | ||
import { | ||
snapshot | ||
} from '@harlem/plugin-snapshot'; | ||
|
||
export default function() { | ||
const snap = snapshot('my-store'); | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
4. Apply the snapshot: | ||
```typescript | ||
const snap = snapshot('my-store'); | ||
|
||
snap.apply(); // Apply the snapshot over the top of current state | ||
snap.apply(true) // Replace state with the current snapshot | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require('path'); | ||
const build = require('@harlem/build'); | ||
|
||
(async () => { | ||
const cwd = path.resolve('.'); | ||
|
||
return build(cwd, 'index', { | ||
globalName: 'HarlemTraceExtension' | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@harlem/extension-trace", | ||
"amdName": "harlemTrace", | ||
"version": "1.3.2", | ||
"license": "MIT", | ||
"author": "Andrew Courtice <[email protected]>", | ||
"description": "The official trace extension for Harlem", | ||
"homepage": "https://harlemjs.com", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.bundler.esm.js", | ||
"unpkg": "dist/index.min.js", | ||
"types": "dist/index.d.ts", | ||
"source": "src/index.ts", | ||
"keywords": [ | ||
"vue", | ||
"state", | ||
"harlem", | ||
"extension", | ||
"trace" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/andrewcourtice/harlem.git", | ||
"directory": "extensions/trace" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/andrewcourtice/harlem/issues" | ||
}, | ||
"scripts": { | ||
"build": "node build.js", | ||
"prepublish": "yarn build" | ||
}, | ||
"peerDependencies": { | ||
"@harlem/core": "^1.1.2" | ||
}, | ||
"dependencies": { | ||
"@harlem/utilities": "^1.3.2" | ||
}, | ||
"devDependencies": { | ||
"@harlem/core": "^1.3.2", | ||
"@harlem/testing": "^1.3.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export type TraceGate<TValue extends object> = keyof ProxyHandler<TValue>; | ||
export type TraceCallback<TValue extends object> = (result: TraceResult<TValue>) => void; | ||
|
||
export type GateMap<TValue extends object = any> = { | ||
[TGate in TraceGate<TValue>]?: (callback: TraceCallback<TValue>, options: TraceOptions<TValue>) => ProxyHandler<TValue>[TGate]; | ||
} | ||
|
||
export interface TraceOptions<TValue extends object> { | ||
gates: TraceGate<TValue>[]; | ||
paths: PropertyKey[]; | ||
hasGetGate: boolean; | ||
} | ||
|
||
export interface TraceResult<TValue extends object> { | ||
gate: TraceGate<TValue>; | ||
paths: PropertyKey[]; | ||
oldValue: unknown; | ||
newValue: unknown; | ||
} | ||
|
||
export interface TraceListener { | ||
dispose(): void; | ||
} | ||
|
||
export interface Options { | ||
autoStart: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
getStore, | ||
bootstrap, | ||
} from '@harlem/testing'; | ||
|
||
import traceExtension from '../src'; | ||
|
||
describe('Trace Extension', () => { | ||
|
||
const getInstance = () => getStore({ | ||
extensions: [ | ||
traceExtension(), | ||
], | ||
}); | ||
|
||
let instance = getInstance(); | ||
|
||
beforeAll(() => bootstrap()); | ||
beforeEach(() => instance = getInstance()); | ||
afterEach(() => instance.store.destroy()); | ||
|
||
test('Run a trace', () => { | ||
const { | ||
store, | ||
setUserID, | ||
setUserDetails, | ||
} = instance; | ||
|
||
const { | ||
startTrace, | ||
stopTrace, | ||
onTraceResult, | ||
} = store; | ||
|
||
const callback = jest.fn(); | ||
|
||
onTraceResult(callback); | ||
startTrace(); | ||
|
||
setUserID(5); | ||
setUserDetails({ | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
age: 35, | ||
}); | ||
|
||
stopTrace(); | ||
|
||
expect(callback).toHaveBeenCalledTimes(4); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"src/**/*.ts", | ||
"../../global.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters