redux-saga-final-form
Advanced tools
Comparing version 2.1.0-3 to 2.1.0-4
@@ -5,29 +5,19 @@ "use strict"; | ||
const react_redux_1 = require("react-redux"); | ||
const storeMap = new WeakMap(); | ||
let promiseCounter = 1; | ||
const promiseMap = new Map(); | ||
function useListener(startActionType, resolveActionType, rejectActionType, setPayload) { | ||
const dispatch = react_redux_1.useDispatch(); | ||
return (payload) => { | ||
const action = { | ||
type: startActionType, | ||
payload: setPayload?.(payload) ?? payload | ||
}; | ||
if (!storeMap.has(dispatch)) { | ||
storeMap.set(dispatch, new Map()); | ||
} | ||
const pendingCallbacks = storeMap.get(dispatch); | ||
return new Promise((resolve, reject) => { | ||
if (!pendingCallbacks.has(resolveActionType)) { | ||
pendingCallbacks.set(resolveActionType, []); | ||
} | ||
if (!pendingCallbacks.has(rejectActionType)) { | ||
pendingCallbacks.set(rejectActionType, []); | ||
} | ||
pendingCallbacks.get(resolveActionType).push({ | ||
callback: resolve, | ||
toClear: rejectActionType, | ||
}); | ||
pendingCallbacks.get(rejectActionType).push({ | ||
callback: reject, | ||
toClear: resolveActionType, | ||
}); | ||
const promiseId = promiseCounter++; | ||
const action = { | ||
type: startActionType, | ||
payload: setPayload?.(payload) ?? payload, | ||
meta: { | ||
final_form_promise: promiseId, | ||
final_form_resolve: resolveActionType, | ||
final_form_reject: rejectActionType, | ||
} | ||
}; | ||
promiseMap.set(promiseId, { resolve, reject }); | ||
dispatch(action); | ||
@@ -38,14 +28,29 @@ }); | ||
exports.useListener = useListener; | ||
const handleListeners = (store) => { | ||
const handleListeners = () => { | ||
const pendingCallbacks = new Map(); | ||
return next => action => { | ||
const pendingCallbacks = storeMap.get(store.dispatch); | ||
if (pendingCallbacks) { | ||
const cbInfos = pendingCallbacks.get(action.type); | ||
if (cbInfos) { | ||
for (const cbInfo of cbInfos) { | ||
pendingCallbacks.delete(cbInfo.toClear); | ||
cbInfo.callback(action.payload); | ||
} | ||
if (action.meta?.final_form_promise) { | ||
if (!pendingCallbacks.has(action.meta.final_form_resolve)) { | ||
pendingCallbacks.set(action.meta.final_form_resolve, []); | ||
} | ||
if (!pendingCallbacks.has(action.meta.final_form_reject)) { | ||
pendingCallbacks.set(action.meta.final_form_reject, []); | ||
} | ||
pendingCallbacks.get(action.meta.final_form_resolve).push({ | ||
callback: promiseMap.get(action.meta.final_form_promise).resolve, | ||
toClear: action.meta.final_form_reject, | ||
}); | ||
pendingCallbacks.get(action.meta.final_form_reject)?.push({ | ||
callback: promiseMap.get(action.meta.final_form_promise).reject, | ||
toClear: action.meta.final_form_resolve | ||
}); | ||
promiseMap.delete(action.meta.final_form_promise); | ||
} | ||
const cbInfos = pendingCallbacks.get(action.type); | ||
if (cbInfos) { | ||
for (const cbInfo of cbInfos) { | ||
pendingCallbacks.delete(cbInfo.toClear); | ||
cbInfo.callback(action.payload); | ||
} | ||
} | ||
return next(action); | ||
@@ -52,0 +57,0 @@ }; |
{ | ||
"name": "redux-saga-final-form", | ||
"version": "2.1.0-3", | ||
"version": "2.1.0-4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,33 +9,26 @@ import { useDispatch, useStore } from 'react-redux'; | ||
const storeMap = new WeakMap<Dispatch, Map<string, CbInfo[]>>(); | ||
let promiseCounter = 1; | ||
const promiseMap = new Map<number, { | ||
resolve: Function, | ||
reject: Function, | ||
}>() | ||
export function useListener(startActionType: string, resolveActionType: string, rejectActionType: string, setPayload?: (payload: any) => Object) { | ||
const dispatch = useDispatch(); | ||
return (payload: any) => { | ||
const action = { | ||
type: startActionType, | ||
payload: setPayload?.(payload) ?? payload | ||
}; | ||
if (!storeMap.has(dispatch)) { | ||
storeMap.set(dispatch, new Map()) | ||
} | ||
const pendingCallbacks = storeMap.get(dispatch)!; | ||
return new Promise((resolve, reject) => { | ||
if (!pendingCallbacks.has(resolveActionType)) { | ||
pendingCallbacks.set(resolveActionType, []); | ||
} | ||
if (!pendingCallbacks.has(rejectActionType)) { | ||
pendingCallbacks.set(rejectActionType, []); | ||
} | ||
pendingCallbacks.get(resolveActionType)!.push({ | ||
callback: resolve, | ||
toClear: rejectActionType, | ||
}); | ||
pendingCallbacks.get(rejectActionType)!.push({ | ||
callback: reject, | ||
toClear: resolveActionType, | ||
}); | ||
const promiseId = promiseCounter ++; | ||
const action = { | ||
type: startActionType, | ||
payload: setPayload?.(payload) ?? payload, | ||
meta: { | ||
final_form_promise: promiseId, | ||
final_form_resolve: resolveActionType, | ||
final_form_reject: rejectActionType, | ||
} | ||
}; | ||
promiseMap.set(promiseId, {resolve, reject}) | ||
dispatch(action); | ||
}) | ||
}); | ||
}; | ||
@@ -45,17 +38,33 @@ } | ||
export const handleListeners: Middleware = (store) => { | ||
export const handleListeners: Middleware = () => { | ||
const pendingCallbacks = new Map<string, CbInfo[]>(); | ||
return next => action => { | ||
const pendingCallbacks = storeMap.get(store.dispatch)!; | ||
if (pendingCallbacks) { | ||
const cbInfos = pendingCallbacks.get(action.type)!; | ||
if (cbInfos) { | ||
for (const cbInfo of cbInfos) { | ||
pendingCallbacks.delete(cbInfo.toClear) | ||
cbInfo.callback(action.payload); | ||
} | ||
if (action.meta?.final_form_promise) { | ||
if (!pendingCallbacks.has(action.meta.final_form_resolve)) { | ||
pendingCallbacks.set(action.meta.final_form_resolve, []); | ||
} | ||
if (!pendingCallbacks.has(action.meta.final_form_reject)) { | ||
pendingCallbacks.set(action.meta.final_form_reject, []); | ||
} | ||
pendingCallbacks.get(action.meta.final_form_resolve)!.push({ | ||
callback: promiseMap.get(action.meta.final_form_promise)!.resolve, | ||
toClear: action.meta.final_form_reject, | ||
}) | ||
pendingCallbacks.get(action.meta.final_form_reject)?.push({ | ||
callback: promiseMap.get(action.meta.final_form_promise)!.reject, | ||
toClear: action.meta.final_form_resolve | ||
}); | ||
promiseMap.delete(action.meta.final_form_promise) | ||
} | ||
const cbInfos = pendingCallbacks.get(action.type)!; | ||
if (cbInfos) { | ||
for (const cbInfo of cbInfos) { | ||
pendingCallbacks.delete(cbInfo.toClear) | ||
cbInfo.callback(action.payload); | ||
} | ||
} | ||
return next(action) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13200
183