-
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): started action extension
- Loading branch information
1 parent
ce94b35
commit 9874a8a
Showing
8 changed files
with
226 additions
and
9 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
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,44 @@ | ||
{ | ||
"name": "@harlem/extension-actions", | ||
"amdName": "harlemActions", | ||
"version": "1.3.1", | ||
"license": "MIT", | ||
"author": "Andrew Courtice <[email protected]>", | ||
"description": "The official actions extension for Harlem", | ||
"homepage": "https://harlemjs.com", | ||
"main": "dist/harlem-actions.cjs.js", | ||
"module": "dist/harlem-actions.bundler.esm.js", | ||
"unpkg": "dist/harlem-actions.min.js", | ||
"types": "dist/index.d.ts", | ||
"source": "src/index.ts", | ||
"keywords": [ | ||
"vue", | ||
"state", | ||
"harlem", | ||
"extension", | ||
"actions" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/andrewcourtice/harlem.git", | ||
"directory": "extensions/actions" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/andrewcourtice/harlem/issues" | ||
}, | ||
"scripts": { | ||
"dev": "microbundle watch --raw", | ||
"build": "node build.js", | ||
"prepublish": "yarn build" | ||
}, | ||
"peerDependencies": { | ||
"@harlem/core": "^1.1.2", | ||
"vue": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@harlem/build": "^1.3.1", | ||
"@harlem/core": "^1.3.1", | ||
"@harlem/testing": "^1.3.1", | ||
"vue": "^3.0.0" | ||
} | ||
} |
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,110 @@ | ||
import { | ||
computed | ||
} from 'vue'; | ||
|
||
import { | ||
Extension, | ||
InternalStore, | ||
createStore, | ||
Mutator | ||
} from '@harlem/core'; | ||
|
||
import type { | ||
Action, | ||
ActionBody, | ||
ActionPredicate, | ||
ActionStoreState, | ||
AddActionInstancePayload, | ||
ComposedAction, | ||
RemoveActionInstancePayload | ||
} from './types'; | ||
|
||
export default ((store: InternalStore<ActionStoreState>) => { | ||
|
||
store.write('$add-actions', '$actions-extension', state => { | ||
state.$actions = {}; | ||
}); | ||
|
||
const addActionInstance = store.mutation<AddActionInstancePayload>('$add-action-instance', (state, payload) => { | ||
const { | ||
actionName, | ||
instanceId, | ||
instancePayload | ||
} = payload; | ||
|
||
if (!state.$actions[actionName]) { | ||
state.$actions[actionName] = new Map<symbol, any>(); | ||
} | ||
|
||
state.$actions[actionName].set(instanceId, instancePayload); | ||
}); | ||
|
||
const removeActionInstance = store.mutation<RemoveActionInstancePayload>('$remove-action-instance', (state, payload) => { | ||
const { | ||
actionName, | ||
instanceId, | ||
} = payload; | ||
|
||
if (!state.$actions[actionName]) { | ||
return; | ||
} | ||
|
||
state.$actions[actionName].delete(instanceId); | ||
}); | ||
|
||
function action<TPayload, TResult = void>(name: string, body: ActionBody<TPayload, TResult>): Action<TPayload, TResult> { | ||
const mutate = (mutator: Mutator<ActionStoreState, undefined, void>) => store.write(name, '$actions-extension', mutator); | ||
|
||
const _action = async (payload: TPayload) => { | ||
const id = Symbol(name); | ||
|
||
addActionInstance({ | ||
actionName: name, | ||
instanceId: id, | ||
instancePayload: payload | ||
}); | ||
|
||
let result: TResult; | ||
|
||
try { | ||
result = await body(payload); | ||
} finally { | ||
removeActionInstance({ | ||
actionName: name, | ||
instanceId: id | ||
}); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
_action.name = name; | ||
|
||
return _action as Action<TPayload, TResult>; | ||
} | ||
|
||
function isActionRunnng<TPayload = any>(name: string, instancePredicate?: ActionPredicate<TPayload>) { | ||
const predicate = instancePredicate || (() => true); | ||
const instances = store.state.$actions[name]; | ||
|
||
return !!instances && Array | ||
.from(instances.values()) | ||
.some(payload => predicate(payload)); | ||
} | ||
|
||
function useAction<TPayload, TResult>(action: Action<TPayload, TResult>): ComposedAction<TPayload, TResult> { | ||
const isRunning = (predicate?: ActionPredicate<TPayload>) => isActionRunnng(action.name, predicate); | ||
|
||
return [ | ||
action, | ||
isRunning | ||
]; | ||
} | ||
|
||
return { | ||
action, | ||
isActionRunnng, | ||
useAction | ||
}; | ||
|
||
}); |
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,26 @@ | ||
export type ActionBody<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload) => Promise<TResult> : (payload: TPayload) => Promise<TResult>; | ||
//export type Action<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload) => Promise<TResult> : (payload: TPayload) => Promise<TResult>; | ||
export type ActionPredicate<TPayload = any> = (payload?: TPayload) => boolean; | ||
export type ComposedAction<TPayload, TResult = void> = [Action<TPayload, TResult>, (predicate: ActionPredicate<TPayload>) => boolean]; | ||
|
||
export interface Action<TPayload, TResult = void> { | ||
(payload?: TPayload): Promise<TResult>; | ||
name: string | ||
} | ||
|
||
export interface ActionStoreState { | ||
$actions: { | ||
[key: string]: Map<symbol, any>; | ||
} | ||
} | ||
|
||
export interface AddActionInstancePayload { | ||
actionName: string; | ||
instanceId: symbol; | ||
instancePayload?: any; | ||
} | ||
|
||
export interface RemoveActionInstancePayload { | ||
actionName: string; | ||
instanceId: symbol; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"packages": [ | ||
"core", | ||
"packages/*", | ||
"extensions/*", | ||
"plugins/*" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"core", | ||
"docs", | ||
"packages/*", | ||
"extensions/*", | ||
"plugins/*" | ||
], | ||
"scripts": { | ||
|