Skip to content

Commit

Permalink
Internal code switched to watch and fire
Browse files Browse the repository at this point in the history
  • Loading branch information
mateussouzaweb committed Mar 21, 2022
1 parent e2dcc95 commit b0b33cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
23 changes: 6 additions & 17 deletions src/extra/http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fire, watch } from "../core/observers"

declare interface HTTPRequest extends RequestInit {
method: string
url: string
Expand All @@ -13,23 +15,20 @@ declare interface HTTPResult {

declare type HTTPCallback = (details: HTTPRequest | HTTPResult) => void | Promise<void>

let _requestBefore: Array<HTTPCallback> = []
let _requestAfter: Array<HTTPCallback> = []

/**
* Add interceptor callback before each HTTP request
* @param callback
*/
function interceptBefore(callback: HTTPCallback) {
_requestBefore.push(callback)
watch('HTTPInterceptBefore', callback)
}

/**
* Add interceptor callback after each HTTP request
* @param callback
*/
function interceptAfter(callback: HTTPCallback) {
_requestAfter.push(callback)
watch('HTTPInterceptAfter', callback)
}

/**
Expand All @@ -49,17 +48,7 @@ async function request(method: string, url: string, data?: BodyInit, headers?: H
headers: headers
}

const runInterceptors = async (callbacks: Array<HTTPCallback>, data: HTTPRequest | HTTPResult) => {
for (const callback of callbacks) {
try {
await callback.apply({}, [data])
} catch (error) {
return Promise.reject(error)
}
}
}

await runInterceptors(_requestBefore, request)
await fire('HTTPInterceptBefore', request)
const options = Object.assign({}, request)

delete options.url
Expand Down Expand Up @@ -111,7 +100,7 @@ async function request(method: string, url: string, data?: BodyInit, headers?: H
body: body
}

await runInterceptors(_requestAfter, details)
await fire('HTTPInterceptAfter', details)

if (!response.ok) {
throw details
Expand Down
23 changes: 6 additions & 17 deletions src/extra/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { on } from "../core/events"
import { fire, watch } from "../core/observers"

declare interface RoutePath {
path: string
Expand All @@ -17,8 +18,6 @@ declare interface RouteChange {
declare type RouteCallback = (change: RouteChange) => void | Promise<void>

let _routes: Array<RoutePath> = []
let _routeBefore: Array<RouteCallback> = []
let _routeAfter: Array<RouteCallback> = []
let _active: RoutePath

const _options = {
Expand All @@ -45,15 +44,15 @@ const _options = {
* @param callback
*/
function beforeChange(callback: RouteCallback) {
_routeBefore.push(callback)
watch('RouteChangeBefore', callback)
}

/**
* Add callback after each route transition
* @param callback
*/
function afterChange(callback: RouteCallback) {
_routeAfter.push(callback)
watch('RouteChangeAfter', callback)
}

/**
Expand Down Expand Up @@ -270,18 +269,8 @@ function active(): RoutePath {
*/
async function change(toLocation: string, replace?: boolean) {

const runWatchers = async (callbacks: Array<RouteCallback>, change: RouteChange) => {
for (const callback of callbacks) {
try {
await callback.apply({}, [change])
} catch (error) {
return Promise.reject(error)
}
}
}

const next = match(toLocation)
if( next !== null ){
if (next !== null) {
next.location = toLocation
}

Expand All @@ -292,7 +281,7 @@ async function change(toLocation: string, replace?: boolean) {
replace: replace
}

await runWatchers(_routeBefore, change)
await fire('RouteChangeBefore', change)

if (change.replace) {
_options.prevent = true
Expand All @@ -308,7 +297,7 @@ async function change(toLocation: string, replace?: boolean) {

_active = (change.next) ? change.next : null

await runWatchers(_routeAfter, change)
await fire('RouteChangeAfter', change)

}

Expand Down

0 comments on commit b0b33cc

Please sign in to comment.